using System.Collections.Generic;
using GameFramework.ObjectPool;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce
{
    /// <summary>
    /// 战斗飘字效果组件
    /// </summary>
    public class HpTextComponent : GameFrameworkComponent
    {
        /// <summary>
        /// 飘字Item
        /// </summary>
        [SerializeField]
        private HPTextItem m_HpTextItem = null;

        /// <summary>
        /// 生成飘字Item的父节点
        /// </summary>
        [SerializeField]
        private Transform m_HpTextInstanceRoot = null;

        /// <summary>
        /// 对象池容量
        /// </summary>
        [SerializeField]
        private int m_InstancePoolCapacity = 16;

        /// <summary>
        /// 飘字对象池
        /// </summary>
        private IObjectPool<HpTextItemObject> m_HpTextObjectPool;

        /// <summary>
        /// 当前显示的飘字Item
        /// </summary>
        private List<HPTextItem> m_ActiveHpTextItems;
        
        /// <summary>
        /// 缓存的画布
        /// </summary>
        private Canvas m_CachedCanvas;

        private void Start()
        {
            if (m_HpTextInstanceRoot == null)
            {
                Log.Error("You must set HP Text instance root first.");
                return;
            }
            
            m_CachedCanvas = m_HpTextInstanceRoot.GetComponent<Canvas>();
            m_HpTextObjectPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool<HpTextItemObject>("HPTextItem", m_InstancePoolCapacity);
            m_ActiveHpTextItems = new List<HPTextItem>();
        }

        /// <summary>
        /// 显示对象
        /// </summary>
        /// <param name="hpTextData"></param>
        /// <param name="formPosition"></param>
        /// <param name="toVector3"></param>
        public void Show(HpTextData hpTextData , Vector3 formPosition,Vector3 toVector3)
        {
            HPTextItem hpTextItem = Create();
            
            hpTextItem.Init(hpTextData,formPosition,toVector3);
        }

        /// <summary>
        /// 隐藏对象
        /// </summary>
        /// <param name="hpTextItem"></param>
        public void Hidden(HPTextItem hpTextItem)
        {
            hpTextItem.Reset();
            m_ActiveHpTextItems.Remove(hpTextItem);
            m_HpTextObjectPool.Unspawn(hpTextItem);
        }
        
        

        /// <summary>
        /// 创建一个对象
        /// </summary>
        /// <returns></returns>
        private HPTextItem Create()
        {
            HPTextItem hpTextItem = null;

            HpTextItemObject hpTextItemObject = m_HpTextObjectPool.Spawn();

            if (hpTextItemObject != null)
            {
                hpTextItem = (HPTextItem)hpTextItemObject.Target;  //如果对象池中有一个对象 直接获取
            }
            else
            {
                //否则新生成一个对象
                hpTextItem = Instantiate(m_HpTextItem);
                Transform transform = hpTextItem.GetComponent<Transform>();
                transform.SetParent(m_HpTextInstanceRoot);
                transform.localScale = Vector3.one;
                m_HpTextObjectPool.Register(HpTextItemObject.Create(hpTextItem), true);
            }
            return hpTextItem;
        }
    }
}